#include "drawwidget.h"
#include <QDebug>
#include <cmath>

DrawWidget::DrawWidget(QWidget *parent)
    : QWidget(parent) // 初始化指向MainWindow对象的指针
{
    setAutoFillBackground(true); // 设置自动填充背景
    setPalette(QPalette(Qt::white)); // 设置背景颜色为白色
}

void DrawWidget::paintEvent(QPaintEvent *event)
{
    mPainter.begin(this);
    mPainter.setRenderHint(QPainter::Antialiasing);
    float drawScale = 550 / waferDim;
    qDebug() << "drawScale" << drawScale;
    drawOutline(mPainter, drawScale);
    drawChip(mPainter, drawScale);
    mPainter.end();
}

void DrawWidget::saveImage()
{
    float scale = 10;
    imageLenght = waferDim * scale * 1.1;

    QImage image(imageLenght, imageLenght, QImage::Format_RGB32);
    image.fill(Qt::white);

    QPainter p(&image);
    p.setRenderHint(QPainter::Antialiasing);
    p.setRenderHint(QPainter::TextAntialiasing);
    p.setRenderHint(QPainter::HighQualityAntialiasing);

    drawOutline(p, scale);
    drawChip(p, scale);
    drawText(p, scale / 3);

    image.save("芯片数量估算结果.png");
}

void DrawWidget::startDraw(int count)
{
    qDebug() << "startDraw";
    chipCount = count;
    update();
}

void DrawWidget::drawChip(QPainter &painter, float scale)
{
    qDebug() << "drawChip";
    painter.setPen(QPen(Qt::blue, 0.1));
    painter.setBrush(Qt::blue);

    double s = hSpace > vSpace ? vSpace : hSpace;
    qDebug() << "s=" << s;
    double offset;
    if(s < 0.1){
        offset = 0.1;
    }
    QRect tempRect;
    for (int n = 0; n < pointList.size(); n++) {
        tempRect = QRect((pointList[n].x * scale + startPointX + offset), (pointList[n].y * scale + startPointY) + offset, chipWidth * scale - offset, chipHeight * scale - offset);
        painter.drawRect(tempRect);
    }
}

void DrawWidget::drawOutline(QPainter &painter, float scale)
{
    qDebug() << "drawOutline()" << scale;
    if(scale == 10){
        drawCenterX = imageLenght / 2;
        drawCenterY = imageLenght / 2;
    }else{
        drawCenterX = width() / 2;
        drawCenterY = height() / 2;
    }
    qDebug() << "drawCenterX" << drawCenterX;
    double scaleWaferDim = waferDim * scale;
    double scaleEdgeSpace = edgeSpace * scale;

    startPointX = drawCenterX - scaleWaferDim / 2;
    startPointY = drawCenterY - scaleWaferDim / 2;

    QRect rect(startPointX, startPointY, scaleWaferDim, scaleWaferDim);

    //绘制外圈圆
    painter.setPen(QPen(Qt::yellow, 0.1));
    painter.setBrush(Qt::yellow);
    painter.drawEllipse(rect);

    //绘制内圈圆
    painter.setPen(QPen(Qt::red, 1));
    painter.setBrush(Qt::red);
    painter.drawEllipse(startPointX + scaleEdgeSpace, startPointY + scaleEdgeSpace, scaleWaferDim - (2 * scaleEdgeSpace), scaleWaferDim - (2 * scaleEdgeSpace));

    //绘制底部的弦
    painter.setPen(QPen(Qt::yellow, 0.1));
    painter.setBrush(Qt::yellow);

    double angleA, startPoint, endPoint, r;
    r = scaleWaferDim / 2;
    angleA = acos((r - flatHeight * scale) / r) * 180 / M_PI;
    startPoint = (270 - angleA) * 16;
    endPoint = (2 * angleA) * 16;
    painter.drawChord(rect, startPoint, endPoint);

    painter.setPen(QPen(Qt::black, 1));
    painter.setBrush(Qt::NoBrush);
    painter.drawEllipse(rect);

    //绘制十字线
    //    painter.setPen(QPen(Qt::white, 1));
    //    painter.drawLine(QLine(drawCenterX, startPointY, drawCenterX, startPointY + scaleWaferDim));
    //    painter.drawLine(QLine(startPointX, drawCenterY, startPointX + scaleWaferDim, drawCenterY));

    //
    painter.setPen(QPen(Qt::white, 1));
    painter.setBrush(Qt::white);
    QPoint point1(drawCenterX, drawCenterY + scaleWaferDim / 2 - edgeSpace * scale);
    QPoint point2(drawCenterX - edgeSpace * scale / 2, drawCenterY + scaleWaferDim / 2);
    QPoint point3(drawCenterX + edgeSpace * scale / 2, drawCenterY + scaleWaferDim / 2);
    QPolygon polygon;
    polygon << point2 << point1 << point3;
    painter.drawPolygon(polygon);
    QPoint point4(drawCenterX - edgeSpace * scale / 2, drawCenterY + scaleWaferDim / 2 + edgeSpace);
    QPoint point5(drawCenterX + edgeSpace * scale / 2, drawCenterY + scaleWaferDim / 2 + edgeSpace);

    painter.drawRect(drawCenterX - edgeSpace * scale / 2, drawCenterY + scaleWaferDim / 2, edgeSpace * scale, edgeSpace * scale);

    painter.setPen(QPen(Qt::black, 1));

    painter.drawLine(point2, point1);
    painter.drawLine(point1, point3);
}

void DrawWidget::drawText(QPainter &painter, int scale)
{
    qDebug() << "drawText()";
    int textSize = 4, nextLine = textSize * 1.6, textX = 10, textY = 10;
    QFont font;
    font.setPointSize(textSize * scale);
    painter.setFont(font);
    painter.setPen(Qt::black);
    painter.drawText(QPoint(textX * scale, textY * scale), QString("芯片宽度:%1毫米").arg(chipWidth));
    painter.drawText(QPoint(textX * scale, (textY += nextLine) * scale), QString("芯片高度:%1毫米").arg(chipHeight));
    painter.drawText(QPoint(textX * scale, (textY += nextLine) * scale), QString("水平间距:%1毫米").arg(hSpace));
    painter.drawText(QPoint(textX * scale, (textY += nextLine) * scale), QString("垂直间距:%1毫米").arg(vSpace));
    painter.drawText(QPoint(textX * scale, (textY += nextLine) * scale), QString("晶圆直径:%1毫米").arg(waferDim));
    painter.drawText(QPoint(textX * scale, (textY += nextLine) * scale), QString("边界距离:%1毫米").arg(edgeSpace));
    painter.drawText(QPoint(textX * scale, (textY += nextLine) * scale), QString("平边高度:%1毫米").arg(flatHeight));
    painter.setPen(Qt::red);
    painter.drawText(QPoint(textX * scale, (textY += nextLine) * scale), QString("芯片总数:%1个").arg(chipCount));
}
